<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html class="tutorial" lang="en">
<head>
	<title>MonoTouch Examples :: RemoteInfo</title>
    <link rel="stylesheet" type="text/css" href="../css/default.css" />
    <link rel="stylesheet" type="text/css" href="../css/remoteinfo.css" />
</head>
<body class="large">
	<a id="title-img" href="http://www.servicestack.net"><img src="../img/MonoTouch-ServiceStack.png" alt="MonoTouch ServiceStack"></a>
	<h2>Calling Web Services from MonoTouch</h2>

	<h3 class="step"><a href="default.htm">MonoTouch tutorial</a> Step 1 - Getting Started</h3>

	<div class="section">
	<p>
		In this tutorial I will show how easy it is to create Web Services with <a href="http://www.servicestack.net">Service Stack</a>
		and call them from a <a href="http://www.monotouch.net">MonoTouch</a> application.
	</p>
	<p>
		You can download the full source code for this tutorial at:
		<a href="http://servicestack.googlecode.com/files/RemoteInfo.zip">http://servicestack.googlecode.com/files/RemoteInfo.zip</a>.
		<br />
		Alternatively, you can browse all the source code online at:
		<a href="http://code.google.com/p/servicestack/source/browse/#svn/trunk/MonoTouch.Examples/RemoteInfo">
			http://code.google.com/p/servicestack/source/browse/#svn/trunk/MonoTouch.Examples/RemoteInfo
		</a>
	</p>

	<p>
		For simplicity we'll start with an existing Mono Develop solution which has already been configured
		to host Web Services using Service Stack.
		<br />
		<b>RemoteInfo</b> is an example project that exposes information about a servers'
		file / directory structure via XML and JSON Web Services:
	</p>

	<dl id="sln-info">
		<dd>
			<h3>Clients</h3>
			<h4>Example clients that call the Web Services</h4>
			<ul>
				<li>
					<strong>RemoteInfo.Tests.ConsoleClient</strong> - From a simple Console App
				</li>
				<li>
					<strong>RemoteInfoClient</strong> - From a MonoTouch client
				</li>
			</ul>

			<h3>Hosts</h3>
			<h4>Different ways to host ServiceStack Web Services</h4>
			<ul>
				<li>
					<strong>RemoteInfo.Host.Console</strong> - Running within a Console App (without a web server)
				</li>
				<li>
					<strong>RemoteInfo.Host.Web</strong> - In a standard .NET Web Application
				</li>
			</ul>

			<h3>Application Code</h3>
			<ul>
				<li>
					<strong>RemoteInfo.ServiceInterface</strong> - Implementations for each Web Service
				</li>
				<li>
					<strong>RemoteInfo.ServiceModel</strong> - Request and Response DataContract
					<acronym title="Data Transfer Objects">DTO</acronym>'s (also shared by .NET web service clients)
				</li>
			</ul>
		</dd>
		<dd>
			<br />
			The configuration for the host projects are ultimately the same except that one is configured to be run in a
			Console Application while the other allows it to be hosted within a Web Server configured to run ASP.NET
			applications which include:
			<ul>
				<li>IIS 6.0/7.0</li>
				<li><a href="http://httpd.apache.org/">Apache</a> + <a href="http://www.mono-project.com/Mod_mono">Mod_Mono</a></li>
				<li><a href="http://www.mono-project.com/ASP.NET">XSP</a></li>
				<li><a href="http://www.mono-project.com/FastCGI">FastCGI</a> enabled web servers like
					<a href="http://nginx.net/">Nginx</a>,
					<a href="http://www.cherokee-project.com/">Cherokee</a>, etc</li>
			</ul>
			<br />
			After the hosts are configured they no longer need to be modified as adding new Web Services only require adding to
			the 'Application Code' projects.
		</dd>
	</dl>

	<img src="../img/remote-info-sln.png" style="float:left;">

	<hr />

	<h4>Configuring ServiceStack - Modifying AppHost.cs</h4>
	<p>
		The configuration of a ServiceStack Web Service Host is held in the <b class="cs">AppHost.cs</b> class.
		The contents of RemoteInfo's AppHost.cs is included below. <br />
		Configuring ServiceStack as seen in this example is pretty easy, generally the only things you need to do is:
		<ul>
			<li>(Line 23) - Tell ServiceStack which assemblies your web service handlers (i.e. implementation) can be found</li>
			<li>(Line 29) - Use ServiceStack's built-in <a href="http://funq.codeplex.com/">Funq</a> IOC Container to register
				dependencies you want injected into your services</li>
		</ul>
		<br />
	</p>

	<img src="../img/AppHost-cs.png" class="left" />

	<p class="clear">
		The only thing left to do is to get your configuration to run on start-up which can be done in a Web Application done by adding
<code>
	protected void Application_Start(object sender, EventArgs e)
	{
		var appHost = new AppHost();
		appHost.Init();
	}
</code>
		to your <b class="cs">Global.asax.cs</b>.
	</p>

	<p>
		If everything is configured properly you should be able to go to the base of your Web Application directory
		(RemoteInfo.Host.Web) in Terminal and run xsp from the Command Line:
	</p>
	<img src="../img/xsp-running.png" alt="Running XSP from the Command Line"/>

	<p>
		If all went well and you didn't get any errors going to <a href="http://localhost:8080">http://localhost:8080</a>
		should give you a splash page for your Web Services:
	</p>
	<img src="../img/ws-splash.png" alt="Web Services splash page"/>

	<p>
		This splash page contains a list of all Web Services available from this Web Application.
		On the right side of each 'Operation' is the list of different end points that your web service is available from.
		Other info available includes links to example code on different ways you can call ServiceStack Web Services.
		As well as the XSD's and WSDL for your web services - useful if you use other tooling to examine and call your Web Services.
	</p>
	<p>
		Clicking on the <a href="http://localhost:8080/Public/Xml/Metadata?op=GetDirectoryInfo">XML</a> link for a particular
		Operation shows a complete example (including HTTP traffic) on how to call the selected web service as well as
		the response to expect. I find this useful for times when I want to check the output of a Web Service without
		 having to load Mono Develop or VS.NET to find out.
	</p>
	<img src="../img/ws-splash-xml.png" alt="GetDirectoryInfo XML example"/>

	</div>

	<div class="page-nav">
		<div class="next">
			<a href="step2.htm">Step 2 - Calling the GetDirectoryInfo Web Service &gt;</a>
		</div>
	</div>

<div id="footer">
	Copyright &copy; 2010 Liquidbit Limited. All rights reserved.
</div>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-7722718-7");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>
</html>